<?php
namespace app\index\controller;

use think\Controller;
use think\Cookie;

class Setting extends Controller
{
    public static $userid;
    public function _initialize()
    {
        if (!Cookie::has('uid')) {
            $this->redirect('user/index');
        }
        self::$userid = cookie('uid');
//        $this->assign('sysConfig', $sysConfig);
    }

    /**
     * 个人中心设置
     */
    public function personal(){
        $userinfo = db('user')->where('id',self::$userid)->find();
        $this->assign('userinfo',$userinfo);
        return view();
    }

    /**
     * 修改昵称
     */
    public function changeprofile(){
        $data = input('post.nickname');
       $res = db('user')->where('id',self::$userid)->setField('nickname',$data);
       if($res){
           return $this->result('',1,'修改成功');
       }
    }

    /**
     * 修改密码
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function changepwd(){
        $data = input('post.');
       $userinfo = db('user')->where('id',self::$userid)->find();
        if(md5($data['oldpass'])!==$userinfo['pwd']){
            return $this->result('',0,'原密码输入错误');
        }
        if($data['newpass']!==$data['renewpass']){
            return $this->result('',0,'两次密码输入不一致');
        }
       $res = db('user')->where('id',self::$userid)->setField('pwd',md5($data['newpass']));
        if($res){
            return $this->result('',1,'修改成功');
        }else{
            return $this->result('',0,'修改失败');
        }

    }

    /**
     * 随机四位验证码
     */
     public function ranum($length = 4){
            return str_pad(mt_rand(0, pow(10, $length) - 1), $length, '0', STR_PAD_LEFT);
     }



}
